package com.malou.server.ventas.gasto;

import java.sql.SQLException;
import java.util.List;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.malou.client.ventas.gasto.Gasto;
import com.malou.server.base.Conexion;
import com.malou.server.base.util.GxtGridPaginate;
import com.malou.server.ventas.ingresostock.IngresoStocks;

public class Gastos {

	public static Integer tienda_id;
	private static SqlMapClient sqlMap;

	@SuppressWarnings("unchecked")
	public static PagingLoadResult<Gasto> getPaginate(GxtGridPaginate paginate) {	      
		
		sqlMap = Conexion.getSqlMap();
		paginate.setLimit();
		
		if(paginate.order.equals("")){
			paginate.order = " order by id desc";
		}
		List<Gasto> data = null;
		try {
			data = sqlMap.queryForList("paginateGasto", paginate);				
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return new BasePagingLoadResult<Gasto>(data, paginate.offset, count(paginate));
	}

	private static Integer count(GxtGridPaginate paginate) {
		Integer count = null;
		try {
			count = (Integer) sqlMap.queryForObject("countGasto", paginate);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

	public static boolean save(List<Gasto> list) {
		boolean retorno = true;		
        sqlMap = Conexion.getSqlMap();
        try {
        	
        	sqlMap.startTransaction();        		
        		
    		sqlMap.startBatch();
    		IngresoStocks.sqlMap = sqlMap;
    		  		
    		for(Gasto me : list){    			
    			me.setTienda_id(tienda_id);
    			sqlMap.insert("insertGasto", me);								
    		}        	
    		sqlMap.executeBatch();    		
    		
    		sqlMap.commitTransaction();
        	
        } catch (SQLException e) {
        	retorno = false;
			e.printStackTrace();
		}
        
     	try {
			sqlMap.endTransaction();
		} catch (SQLException e) {				
			e.printStackTrace();
		}
         
        return retorno;
	}

}
